﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using CommandLib;

namespace BackEnd.Controllers
{
    public class ImageLastProjectController : Controller
    {
        //
        // GET: /ImageLastProject/
        [CustomAuthorize(Action = Constants.AdminActions.ListImageLastProject)]
        public ActionResult Index()
        {
            ImageLastProjectModel imageLastProjectModel = new ImageLastProjectModel();
            imageLastProjectModel.ListImageLastProject = new ImageLastProjectBL().GetAll();
            return View(imageLastProjectModel);
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateImageLastProject)]
        public ActionResult Create()
        {
            ImageLastProjectModel imageLastProjectModel = new ImageLastProjectModel();
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_ARTIST, Name = "Image Artist" });
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_ARTWORK, Name = "Image ArtWork" });
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_SIGNATURES, Name = "Image Signatures" });
            return View(imageLastProjectModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Create(string type, string image, string redirectUrl, string info)
        {
            string user = (string)Session[Common.Constants.SessionKeys.UserInfo];
            string permissionActions = (string)Session[Common.Constants.SessionKeys.PermissionActions];
            bool hasPermission = false;
            if (String.IsNullOrEmpty(user))
            {
                return Json(-1, JsonRequestBehavior.AllowGet);
            }

            hasPermission = permissionActions != null && permissionActions.Contains(Constants.AdminActions.CreateImageLastProject);
            if (hasPermission == false)
            {
                return Json(-2, JsonRequestBehavior.AllowGet);
            }
            new ImageLastProjectBL().Insert(new ImageLastProject { Image=image, ImageUrl= Config.FolderImageLastProject, Type= Convert.ToInt32(type), RedirectUrl=redirectUrl, Info=info });
            return Json(1, JsonRequestBehavior.AllowGet);
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditImageLastProject)]
        public ActionResult Edit(int id)
        {
            ImageLastProjectModel imageLastProjectModel = new ImageLastProjectModel();
            ImageLastProject imageLastProject = new ImageLastProjectBL().GetById(id);
            if (imageLastProject != null)
            {
                imageLastProjectModel.Id = imageLastProject.Id;
                imageLastProjectModel.Image = imageLastProject.Image;
                imageLastProjectModel.ImageUrl = imageLastProject.ImageUrl;
                imageLastProjectModel.Type = imageLastProject.Type;
                imageLastProjectModel.RedirectUrl = imageLastProject.RedirectUrl;
                imageLastProjectModel.Info = imageLastProject.Info;
            }            
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_ARTIST, Name = "Image Artist" });
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_ARTWORK, Name = "Image ArtWork" });
            imageLastProjectModel.ListImageLastProjectType.Add(new ImageLastProjectTypeModel { Id = Constant.PROJECT_IMAGE_SIGNATURES, Name = "Image Signatures" });
            return View(imageLastProjectModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Edit(string id, string type, string image, string redirectUrl, string info)
        {
            string user = (string)Session[Common.Constants.SessionKeys.UserInfo];
            string permissionActions = (string)Session[Common.Constants.SessionKeys.PermissionActions];
            bool hasPermission = false;
            if (String.IsNullOrEmpty(user))
            {
                return Json(-1, JsonRequestBehavior.AllowGet);
            }

            hasPermission = permissionActions != null && permissionActions.Contains(Constants.AdminActions.EditImageLastProject);
            if (hasPermission == false)
            {
                return Json(-2, JsonRequestBehavior.AllowGet);
            }
            new ImageLastProjectBL().Update(new ImageLastProject { Id = Convert.ToInt32(id), Image = image, ImageUrl = Config.FolderImageLastProject, Type = Convert.ToInt32(type), RedirectUrl = redirectUrl, Info = info });
            return Json(1, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteImageLastProject)]
        public JsonResult Delete(string listId)
        {
            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new ImageLastProjectBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

        [ValidateInput(false)]
        public JsonResult UploadFile()
        {
            string imageName = string.Empty;
            string fileName = string.Empty;
            int errorCode = 100;
            try
            {
                var files = Request.Files;
                if (files.Count > 0)
                {
                    if (!Directory.Exists(Config.FolderImageLastProject))
                    {
                        Directory.CreateDirectory(Config.FolderImageLastProject);
                    }
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        imageName = file.FileName;
                        var lastIndexOfDot = imageName.LastIndexOf(".");
                        if (lastIndexOfDot > -1)
                        {
                            fileName = string.Format("{0}_{1}{2}", file.FileName.Substring(0, lastIndexOfDot - 1),
                                DateTime.Now.Ticks.ToString(),
                                file.FileName.Substring(lastIndexOfDot));
                            string filePath = Path.Combine(
                                Config.FolderImageLastProject,
                                fileName);
                            file.SaveAs(filePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorCode = -999;
            }
            return Json(new { errorCode, fileName }, JsonRequestBehavior.AllowGet);
        }
    }
}
